<?php
namespace backend\modules\business\methods\cashier;

use backend\modules\business\methods\base\Basemethod;
use common\models\BusEmployee;
use common\models\BusEmployeeReward;
use common\models\BusMemberBeautycard;
use common\models\BusMemberConsumeList;
use common\models\BusService;
use common\models\BusServiceExtend;
use common\models\Status;
use common\models\BusMember;
use yii\data\Pagination;
use common\models\BusMemberConsume;
use common\models\BusMemberRefund;
use common\models\BusRepertory;
use common\extended\system\Util;
use common\models\BusGoodsDec;

/**
 * 统计
 * @author      futao
 * @date        2016-3-14
 * @version     2.0
 * @copyright   Copyright 2016 meelier.com
 */
class Statistics extends Basemethod
{
    /**
     * 应收欠款
     * @date: 2016-3-14
     * @author: futao
     */
    public function debtList()
    {
        $where = $this->where;
        $where[] = 'del_flag = :flag';
        $bind[':flag'] = Status::NORMAL;
        $where[] = 'member_debt > 0';
        $whereStr = implode(' and ', $where);
        $query = BusMember::find()
            ->where($whereStr, $bind);
        $queryCount = clone $query;
        $page = new Pagination(['totalCount' => $queryCount->count(), 'pageSize' => $this->pageNavLimit]);
        $list = $query
            ->offset($page->offset)
            ->limit($page->limit)
            ->orderBy('member_debt desc')
            ->asArray()
            ->all();
        return $this->render('debtlist', ['list' => $list, 'page' => $page]);
    }

    /**
     * 欠款统计
     * @date: 2016-3-14
     * @author: futao
     */
    public function debtStatistics()
    {
        $where = $this->where;
        $where[] = 'del_flag = :flag';
        $bind[':flag'] = Status::NORMAL;
        $whereStr = implode(' and ', $where);
        $debtCount = BusMember::find()
            ->select('sum(member_debt) as debt_count')
            ->where($whereStr, $bind)
            ->asArray()->all();
        $balanceCount = BusMember::find()
            ->select('sum(member_balance) as balance_count')
            ->where($whereStr, $bind)
            ->asArray()->all();
        $giveBalancecount = BusMember::find()
            ->select('sum(member_give_balance) as give_balance_count')
            ->where($whereStr, $bind)
            ->asArray()->all();

        $cardCount = BusMemberBeautycard::find()
            ->select("sum(total_price / total_times * remain_times) as card_count")
            ->where("shop_id = :shop_id and is_give = 0", [':shop_id' => $this->userInfo->shop_id])
            ->asArray()->all();
        $info = [
            'debt_count' => $debtCount[0]['debt_count'],
            'balance_count' => $balanceCount[0]['balance_count'],
            'give_balance_count' => $giveBalancecount[0]['give_balance_count'],
            'card_count' => $cardCount[0]['card_count']
        ];
        return $this->render('debtstatistics', ['info' => $info]);
    }

    /**
     * 收银明细
     * @date: 2016-3-14
     * @author: futao
     */
    public function cashierList()
    {
        $where = $this->where;
        $where[] = 'consume_type != :type';
        $bind[':type'] = '退款';
        $whereStr = implode(' and ', $where);
        $query = BusMemberConsume::find()
            ->where($whereStr, $bind);
        $queryCount = clone $query;
        $page = new Pagination(['totalCount' => $queryCount->count(), 'pageSize' => $this->pageNavLimit]);
        $list = $query
            ->offset($page->offset)
            ->limit($page->limit)
            ->orderBy('consume_id desc')
            ->asArray()
            ->all();
        foreach ($list as $k => $v) {
            $list[$k]['member_name'] = BusMember::find()->select('member_name')->where("member_id = " . $list[$k]['member_id'])->one();
        }
        return $this->render('cashierlist', ['page' => $page, 'list' => $list]);
    }

    /**
     * 退款明细
     * @date: 2016-3-14
     * @author: futao
     */
    public function refundList()
    {
        $where = $this->where;
        $whereStr = implode(' and ', $where);
        $query = BusMemberRefund::find()
            ->where($whereStr);
        $queryCount = clone $query;
        $page = new Pagination(['totalCount' => $queryCount->count(), 'pageSize' => $this->pageNavLimit]);
        $list = $query
            ->offset($page->offset)
            ->limit($page->limit)
            ->orderBy('refund_id desc')
            ->asArray()
            ->all();
        foreach ($list as $k => $v) {
            $list[$k]['member_name'] = BusMember::find()->select('member_name')->where("member_id = " . $list[$k]['member_id'])->one();
        }
        return $this->render('refundlist', ['page' => $page, 'list' => $list]);
    }

    /**
     * 家居统计
     * @date: 2016-3-15
     * @author: futao
     */
    public function productStatistics()
    {
        $timeBucket = $this->getParam('time_bucket', Util::getDay());
        $timeArr = explode(' 至 ', $timeBucket);
        $startTime = trim($timeArr[0]);
        $endTime = trim($timeArr[1]);
        $where = $this->where;
        $where[] = 'is_sell = 1';
        $whereStr = implode(' and ', $where);
        $query = BusRepertory::find()
            ->where($whereStr);
        $queryCount = clone $query;
        $page = new Pagination(['totalCount' => $queryCount->count(), 'pageSize' => $this->pageNavLimit]);
        $list = $query
            ->offset($page->offset)
            ->limit($page->limit)
            ->orderBy('repertory_id desc')
            ->asArray()
            ->all();
        foreach ($list as $k => $v) {
            $repertoryInfo = BusGoodsDec::find()
                ->select("sum(dec_count) as sell_conut")
                ->where("repertory_id = " . $list[$k]['repertory_id'] . " and dec_type = 5")//dec_time <= '{$endTime}' and dec_time >= '{$startTime}' and
                ->asArray()->all();
            $list[$k]['sell_count'] = $repertoryInfo[0]['sell_conut'];
        }
        return $this->render('productstatistics', ['page' => $page, 'list' => $list]);
    }

    /**
     * 营业流水
     * @date: 2016-3-17
     * @author: futao
     */
    public function saleAmount()
    {
        $timeBucket = $this->getParam('time_bucket', Util::getDay());
        $timeArr = explode(' 至 ', $timeBucket);
        $startTime = trim($timeArr[0]);
        $endTime = trim($timeArr[1]);

        $get_start_time = $this->getParam('start_time');
        $get_end_time =  date("Y-m-d H:i:s",strtotime($this->getParam('end_time')." + 1 days"));

        if (!empty($get_start_time) && !empty($get_end_time)) {
            if (strtotime($get_end_time) > strtotime($get_start_time)) {
                $where = $this->where[0] . " and " . "consume_time >= '$get_start_time' and consume_time <= '$get_end_time'" . " and consume_type != '退款'";
                $refund_where = $this->where[0] . " and " . "consume_time >= '$get_start_time' and consume_time <= '$get_end_time'" . " and consume_type = '退款'";
                $condition = $this->where[0] . " and " . "create_time >= '$get_start_time' and create_time <= '$get_end_time'";
                $come_member_where = $this->where[0] . " and " . "service_endtime >= '$get_start_time' and service_endtime <= '$get_end_time' and service_status = 2";
                $sum_handwork_reward_where = $this->where[0] . " and " . "create_time >= '$get_start_time' and create_time <= '$get_end_time'";
                $bus_member_where = $this->where[0] . " and " . "create_time >= '$get_start_time' and create_time <= '$get_end_time'";
                $bus_service_extend_where = $this->where[0] . " and " . "create_time >= '$get_start_time' and create_time <= '$get_end_time'";
            } else {
                $this->error('信息错误');
            }
        } else {
            $searchTime = $this->getParam('searchTime', 1); //get 参数获取
            if (!in_array($searchTime, [1, 2, 3])) {
                $this->error('信息错误');
            }
            switch ($searchTime) {
                case 1:// 今日
                    $where = $this->where[0] . " and " . "consume_time >= '$startTime' and consume_time <= '$endTime'" . " and consume_type != '退款'";
                    $refund_where = $this->where[0] . " and " . "consume_time >= '$startTime' and consume_time <= '$endTime'" . " and consume_type = '退款'";
                    $condition = $this->where[0] . " and " . "create_time >= '$startTime' and create_time <= '$endTime'";
                    $come_member_where = $this->where[0] . " and " . "service_endtime >= '$startTime' and service_endtime <= '$endTime' and service_status = 2";
                    $sum_handwork_reward_where = $this->where[0] . " and " . "create_time >= '$startTime' and create_time <= '$endTime'";
                    $bus_member_where = $this->where[0] . " and " . "create_time >= '$startTime' and create_time <= '$endTime'";
                    $bus_service_extend_where = $this->where[0] . " and " . "create_time >= '$startTime' and create_time <= '$endTime'";
                    break;
                case 2:// 本月
                    $BeginMonth = date('Y-m-01', strtotime(date("Y-m-d"))); // 本月第一天
                    $lastMonth = date('Y-m-d', strtotime("$BeginMonth +1 month -1 day")) . ' 23:59:59'; // 本月最后一天
                    $where = $this->where[0] . " and " . "consume_time >= '$BeginMonth' and consume_time <= '$lastMonth'" . " and consume_type != '退款'";
                    $refund_where = $this->where[0] . " and " . "consume_time >= '$BeginMonth' and consume_time <= '$lastMonth'" . " and consume_type = '退款'";
                    $condition = $this->where[0] . " and " . "create_time >= '$BeginMonth' and create_time <= '$lastMonth'";
                    $come_member_where = $this->where[0] . " and " . "service_endtime >= '$BeginMonth' and service_endtime <= '$lastMonth' and service_status = 2";
                    $sum_handwork_reward_where = $this->where[0] . " and " . "create_time >= '$BeginMonth' and create_time <= '$lastMonth'";
                    $bus_member_where = $this->where[0] . " and " . "create_time >= '$BeginMonth' and create_time <= '$lastMonth'";
                    $bus_service_extend_where = $this->where[0] . " and " . "create_time >= '$BeginMonth' and create_time <= '$lastMonth'";
                    break;
                case 3:// 本年
                    $beginYear = date("Y-01-01");
                    $lastYear = date("Y-m-d", strtotime("$beginYear +1 year"));
                    $where = $this->where[0] . " and " . "consume_time >= '$beginYear' and consume_time < '$lastYear'" . " and consume_type != '退款'";
                    $refund_where = $this->where[0] . " and " . "consume_time >= '$beginYear' and consume_time < '$lastYear'" . " and consume_type = '退款'";
                    $condition = $this->where[0] . " and " . "create_time >= '$beginYear' and create_time < '$lastYear'";
                    $come_member_where = $this->where[0] . " and " . "service_endtime >= '$beginYear' and service_endtime < '$lastYear' and service_status = 2";
                    $sum_handwork_reward_where = $this->where[0] . " and " . "create_time >= '$beginYear' and create_time < '$lastYear'";
                    $bus_member_where = $this->where[0] . " and " . "create_time >= '$beginYear' and create_time < '$lastYear'";
                    $bus_service_extend_where = $this->where[0] . " and " . "create_time >= '$beginYear' and create_time < '$lastYear'";
                    break;
            }
        }


        $consumeList = BusMemberConsume::find()
            ->select([
                'sum(cash_pay) as cash_pay',          // 现金支付
                'sum(unionpay) as unionpay',   // 刷卡支付
                'sum(give_pay) as give_pay',   // 扣赠金额
                'sum(balance_pay) as balance_pay',   // 卡扣金额
                '(sum(cash_pay) + sum(unionpay)) as consume_amount', // 营业收入
            ])
            ->where($where)
            ->asArray()->all();
        // 退款金额
        $refund = BusMemberConsume::find()->select([
            'sum(consume_amount) as consume_amount',
        ])->where($refund_where)->asArray()->all();

        //	疗程卡
        $finance_type1 = BusMemberConsumeList::find()->select([
            'sum(total_price) as total_price'
        ])->where($condition . ' and finance_type=2')->asArray()->all();

        //	家居产品
        $finance_type2 = BusMemberConsumeList::find()->select([
            'sum(total_price) as total_price'
        ])->where($condition . ' and finance_type=3')->asArray()->all();

        //	有服务项目
        $finance_type5 = BusMemberConsumeList::find()->select([
            'sum(total_price) as total_price'
        ])->where($condition . ' and finance_type=5')->asArray()->all();

        //	无服务项目
        $finance_type4 = BusMemberConsumeList::find()->select([
            'sum(total_price) as total_price'
        ])->where($condition . ' and finance_type=4')->asArray()->all();

        // 到店会员
        $come_member = BusService::find()->where($come_member_where)->asArray()->groupBy("service_member_id")->all();
        $come_member = count($come_member);
        //  新增会员
        $busMembers = BusMember::find()->select(["count(member_id) as member_id"])->where($bus_member_where)->asArray()->all();

        //  消耗金额
        $bus_service_extends = BusEmployeeReward::find()->select(["sum(consume_reward) as consume_amount"])->where($bus_service_extend_where)->asArray()->all();

        // 手工提成
        $sum_handwork_reward = BusEmployeeReward::find()->select(["sum(handwork_reward) as handwork_reward"])->where($sum_handwork_reward_where)->asArray()->all();

        $maopao = $this->maopao([$consumeList[0]['consume_amount'], $consumeList[0]['give_pay'], $consumeList[0]['balance_pay'], $bus_service_extends[0]['consume_amount']]);

        // 最终营业收入
        $consumeList[0]['consume_amount'] = $consumeList[0]['consume_amount'] - $refund[0]['consume_amount'];

        return $this->render('saleamount',
            [
                'consumeList' => $consumeList,
                'come_member' => $come_member,
                'bus_members' => $busMembers,
                'sum_handwork_reward' => $sum_handwork_reward,
                'finance_type1' => $finance_type1,
                'finance_type2' => $finance_type2,
                'finance_type5' => $finance_type5,
                'finance_type4' => $finance_type4,
                'maopao' => $maopao[3],
                'bus_service_extends' => $bus_service_extends
            ]);
    }

    public function saleMap()
    {
        return $this->render('salemap');
    }

    public function saleMapJson()
    {
        $searchTime = $this->getParam('searchTime', 1); //get 参数获取
        $where = $this->where; // 店铺: shop_id = ?
        //  本月
        if ($searchTime == 1) {
            $day = date("d");
            for ($i = 1; $i <= $day; $i++) {
                $startTime = date("Y-m-" . $i);
                $lastTime = date("Y-m-d", strtotime("$startTime +1 day"));
                $condition = $where[0] . " and " . "consume_time >= '$startTime' and consume_time < '$lastTime'" . " and consume_type != '退款'";
                $consume_amount = BusMemberConsume::find()->select(['(sum(cash_pay) + sum(unionpay)) as consume_amount'])->where($condition)->asArray()->all();
                $refund_where = $this->where[0] . " and " . "consume_time >= '$startTime' and consume_time < '$lastTime'" . " and consume_type = '退款'";
                // 退款金额
                $refund = BusMemberConsume::find()->select([
                    'sum(consume_amount) as consume_amount',
                ])->where($refund_where)->asArray()->all();
                // 最终营业收入
                $arr[$i] = $consume_amount[0]['consume_amount'] - $refund[0]['consume_amount'];;
            }
        }

        //  本年
        if ($searchTime == 2) {
            $day = date("m");
            for ($i = 1; $i <= $day; $i++) {
                $startTime = date("Y-" . $i . "-1");
                $lastTime = date("Y-m-d", strtotime("$startTime +1 month"));
                $condition = $where[0] . " and " . "consume_time >= '$startTime' and consume_time < '$lastTime'";
                $consume_amount = BusMemberConsume::find()->select(['(sum(cash_pay) + sum(unionpay)) as consume_amount'])->where($condition)->asArray()->all();
                $refund_where = $this->where[0] . " and " . "consume_time >= '$startTime' and consume_time < '$lastTime'" . " and consume_type = '退款'";
                // 退款金额
                $refund = BusMemberConsume::find()->select([
                    'sum(consume_amount) as consume_amount',
                ])->where($refund_where)->asArray()->all();
                $arr[$i] = $consume_amount[0]['consume_amount'] - $refund[0]['consume_amount'];;
            }
        }
        echo json_encode($arr);
    }

    public function otherCommon($get_start_time, $get_end_time, $searchTime = 1)
    {
        if (!empty($get_start_time) && !empty($get_end_time)) {
            if (strtotime($get_end_time) > strtotime($get_start_time)) {
                $where = $this->where[0] . " and " . "consume_time >= '$get_start_time' and consume_time <= '$get_end_time'"  . " and consume_type != '退款'";
                $refund_where = $this->where[0] . " and " . "consume_time >= '$get_start_time' and consume_time <= '$get_end_time'"  . " and consume_type = '退款'";
//                $condition = $this->where[0] . " and " . "create_time >= '$get_start_time' and create_time <= '$get_end_time'";
                $come_member_where = $this->where[0] . " and " . "service_endtime >= '$get_start_time' and service_endtime < '$get_end_time' and service_status = 2";
                $sum_handwork_reward_where = $this->where[0] . " and " . "create_time >= '$get_start_time' and create_time < '$get_end_time'";
                $bus_member_where = $this->where[0] . " and " . "create_time >= '$get_start_time' and create_time < '$get_end_time'";
                $bus_service_extend_where = $this->where[0] . " and " . "create_time >= '$get_start_time' and create_time < '$get_end_time'";
            } else {
                $this->error('信息错误');
            }
        } else {
//            $searchTime = $this->getParam('searchTime', 1); //get 参数获取
//            if (!in_array($searchTime, [1, 2, 3])) {
//                $this->error('信息错误');
//            }
            switch ($searchTime) {
                case 1:// 今日
                    $startTime = date("Y-m-d") . " 00:00:00";
                    $endTime = date("Y-m-") . (date("d") + 1) . " 00:00:00";
                    $where = $this->where[0] . " and " . "consume_time >= '$startTime' and consume_time < '$endTime'"  . " and consume_type != '退款'";
                    $refund_where = $this->where[0] . " and " . "consume_time >= '$startTime' and consume_time < '$endTime'"  . " and consume_type = '退款'";
//                    $condition = $this->where[0] . " and " . "create_time >= '$startTime' and create_time < '$endTime'";
                    $come_member_where = $this->where[0] . " and " . "service_endtime >= '$startTime' and service_endtime < '$endTime' and service_status = 2";
                    $sum_handwork_reward_where = $this->where[0] . " and " . "create_time >= '$startTime' and create_time < '$endTime'";
                    $bus_member_where = $this->where[0] . " and " . "create_time >= '$startTime' and create_time < '$endTime'";
                    $bus_service_extend_where = $this->where[0] . " and " . "create_time >= '$startTime' and create_time < '$endTime'";
                    break;
                case 2:// 本月
                    $BeginMonth = date('Y-m-01', strtotime(date("Y-m-d"))) . " 00:00:00"; // 本月第一天
                    $lastMonth = date('Y-m-') . (date('d') + 1) . " 00:00:00"; // 本月最后一天
                    $where = $this->where[0] . " and " . "consume_time >= '$BeginMonth' and consume_time < '$lastMonth'"  . " and consume_type != '退款'";
                    $refund_where = $this->where[0] . " and " . "consume_time >= '$BeginMonth' and consume_time < '$lastMonth'"  . " and consume_type = '退款'";
//                    $condition = $this->where[0] . " and " . "create_time >= '$BeginMonth' and create_time < '$lastMonth'";
                    $come_member_where = $this->where[0] . " and " . "service_endtime >= '$BeginMonth' and service_endtime < '$lastMonth' and service_status = 2";
                    $sum_handwork_reward_where = $this->where[0] . " and " . "create_time >= '$BeginMonth' and create_time < '$lastMonth'";
                    $bus_member_where = $this->where[0] . " and " . "create_time >= '$BeginMonth' and create_time < '$lastMonth'";
                    $bus_service_extend_where = $this->where[0] . " and " . "create_time >= '$BeginMonth' and create_time < '$lastMonth'";
                    break;
                case 3:// 本年
                    $beginYear = date("Y-01-01") . " 00:00:00";
                    $lastYear = date("Y-m-d", strtotime("$beginYear +1 year")) . " 00:00:00";
                    $where = $this->where[0] . " and " . "consume_time >= '$beginYear' and consume_time < '$lastYear'"  . " and consume_type != '退款'";
                    $refund_where = $this->where[0] . " and " . "consume_time >= '$beginYear' and consume_time < '$lastYear'"  . " and consume_type = '退款'";
//                    $condition = $this->where[0] . " and " . "create_time >= '$beginYear' and create_time < '$lastYear'";
                    $come_member_where = $this->where[0] . " and " . "service_endtime >= '$beginYear' and service_endtime < '$lastYear' and service_status = 2";
                    $sum_handwork_reward_where = $this->where[0] . " and " . "create_time >= '$beginYear' and create_time < '$lastYear'";
                    $bus_member_where = $this->where[0] . " and " . "create_time >= '$beginYear' and create_time < '$lastYear'";
                    $bus_service_extend_where = $this->where[0] . " and " . "create_time >= '$beginYear' and create_time < '$lastYear'";
                    break;
            }
        }


        $consumeList = BusMemberConsume::find()
            ->select([
                'sum(cash_pay) as cash_pay',          // 现金支付
                'sum(unionpay) as unionpay',   // 刷卡支付
                'sum(give_pay) as give_pay',   // 扣赠金额
                'sum(balance_pay) as balance_pay',   // 卡扣金额
                '(sum(cash_pay) + sum(unionpay)) as consume_amount', // 营业收入
            ])
            ->where($where . " and consume_type != '退款'")
            ->asArray()->all();

        // 退款金额
        $refund = BusMemberConsume::find()->select([
            'sum(consume_amount) as consume_amount',
        ])->where($refund_where)->asArray()->all();

        // 到店会员
        $come_member = BusService::find()->where($come_member_where)->asArray()->groupBy("service_member_id")->all();
        $come_member = count($come_member);
        //  新增会员
        $busMembers = BusMember::find()->select(["count(member_id) as member_id"])->where($bus_member_where)->asArray()->all();
        //  消耗金额
        $bus_service_extends = BusEmployeeReward::find()->select(["sum(consume_reward) as consume_amount"])->where($bus_service_extend_where)->asArray()->all();
        // 手工提成
        $sum_handwork_reward = BusEmployeeReward::find()->select(["sum(handwork_reward) as handwork_reward"])->where($sum_handwork_reward_where)->asArray()->all();

        // 营业收入
        $consumeList[0]['consume_amount'] = $consumeList[0]['consume_amount'] - $refund[0]['consume_amount'];
        return ['consumeList' => $consumeList, 'come_member' => $come_member, 'bus_service_extends' => $bus_service_extends, 'sum_handwork_reward' => $sum_handwork_reward, 'bus_members' => $busMembers];
    }

    public function one()
    {
        $searchTime = $this->getParam('searchTime', 1); //get 参数获取

        $start_time = $this->getParam('start_time');
        $end_time =  date("Y-m-d H:i:s",strtotime($this->getParam('end_time')." + 1 days"));

        $other_common = $this->otherCommon($start_time, $end_time, $searchTime);

        if (!empty($start_time) && !empty($end_time)) {
            if (strtotime($end_time) < strtotime($start_time)) {
                $this->error('参数错误');
            }
        } else {
            if (!in_array($searchTime, [1, 2, 3])) {
                $this->error('信息错误');
            }

            if ($searchTime == 1) {  //  今日
                $start_time = date("Y-m-d") . ' 00:00:00';
                $end_time = date("Y-m-") . (date("d") + 1) . ' 00:00:00';
            }
            if ($searchTime == 2) {  //  本月
                $start_time = date("Y-m-01") . ' 00:00:00';
                $end_time = date("Y-") . (date("m") + 1) . '-01' . ' 00:00:00';
            }
            if ($searchTime == 3) {  //  本年
                $start_time = date("Y-01-01") . ' 00:00:00';
                $end_time = (date("Y") + 1) . '-01-01' . ' 00:00:00';

            }
        }
        $on = " and bus_member_consume." . $this->where[0] . " and bus_member_consume.consume_name != '退款' and bus_member_consume.consume_time >= '$start_time' and bus_member_consume.consume_time < '$end_time'";
        $where = "bus_member." . $this->where[0] . " and bus_member.create_time >= '" . $start_time . "' and bus_member.create_time < '" . $end_time . "'";
        $old_on = " and bus_member_consume." . $this->where[0] . " and bus_member_consume.consume_name != '退款' and bus_member_consume.consume_time >= '$start_time' and bus_member_consume.consume_time< '$end_time'";
        $old_where = " bus_member." . $this->where[0] . " and bus_member.create_time < '" . $start_time . "'";
        // 新会员
        $consumeList = BusMember::find()
            ->select([
                '(sum(bus_member_consume.cash_pay) + sum(bus_member_consume.unionpay)) as consume_amount',
            ])->leftJoin('bus_member_consume', "bus_member.member_id = bus_member_consume.member_id" . $on)
            ->where($where)->asArray()->all();


        // 老会员
        $old_consumeList = BusMember::find()
            ->select([
                '(sum(bus_member_consume.cash_pay) + sum(bus_member_consume.unionpay)) as consume_amount',
            ])->leftJoin('bus_member_consume', "bus_member.member_id = bus_member_consume.member_id" . $old_on)
            ->where($old_where)->asArray()->all();

        return $this->render('one', ['consumeList' => $consumeList, 'old_consumeList' => $old_consumeList, 'other_common' => $other_common]);
    }

    public function two()
    {
        $searchTime = $this->getParam('searchTime', 1); //get 参数获取

        $get_start_time = $this->getParam('start_time');
        $get_end_time =  date("Y-m-d H:i:s",strtotime($this->getParam('end_time')." + 1 days"));

        $other_common = $this->otherCommon($get_start_time, $get_end_time, $searchTime);

        if (!empty($get_start_time) && !empty($get_end_time)) {
            if (strtotime($get_end_time) > strtotime($get_start_time)) {
                $where = 'bus_employee.' . $this->where[0];
                $onBusEmployeeRewards = " and " . "bus_employee_reward.create_time >= '$get_start_time' and bus_employee_reward.create_time < '$get_end_time'";
            } else {
                $this->error('参数错误');
            }
        } else {
            if (!in_array($searchTime, [1, 2, 3])) {
                $this->error('信息错误');
            }

            if ($searchTime == 1) {  //  今日
                $start_time = date("Y-m-d") . ' 00:00:00';
                $end_time = date("Y-m-") . (date("d") + 1) . ' 00:00:00';
                $where = 'bus_employee.' . $this->where[0];
                $onBusEmployeeRewards = " and " . "bus_employee_reward.create_time >= '$start_time' and bus_employee_reward.create_time < '$end_time'";
            }
            if ($searchTime == 2) {  //  本月
                $start_time = date("Y-m-01") . '00:00:00';
                $end_time = date("Y-") . (date("m") + 1) . '-01' . ' 00:00:00';
                $where = 'bus_employee.' . $this->where[0];
                $onBusEmployeeRewards = " and " . "bus_employee_reward.create_time >= '$start_time' and bus_employee_reward.create_time < '$end_time'";
            }
            if ($searchTime == 3) {  //  本年
                $start_time = date("Y-01-01") . ' 00:00:00';
                $end_time = (date("Y") + 1) . '-01-01' . ' 00:00:00';
                $where = 'bus_employee.' . $this->where[0];
                $onBusEmployeeRewards = " and " . "bus_employee_reward.create_time >= '$start_time' and bus_employee_reward.create_time < '$end_time'";
            }
        }
        // 员工消耗业绩
        $BusEmployeeRewards = BusEmployee::find()
            ->select([
                'bus_employee.employee_name',
                'sum(bus_employee_reward.consume_reward) as consume_reward',
            ])->leftJoin('bus_employee_reward', 'bus_employee.employee_id = bus_employee_reward.employee_id ' . $onBusEmployeeRewards)
            ->where($where . ' and bus_employee.del_flag=1')->groupBy('bus_employee.employee_id')
            ->asArray()->all();

        return $this->render('two', ['BusEmployeeRewards' => $BusEmployeeRewards, 'other_common' => $other_common]);
    }

    public function three()
    {
        $searchTime = $this->getParam('searchTime', 1); //get 参数获取

        $get_start_time = $this->getParam('start_time');
        $get_end_time =  date("Y-m-d H:i:s",strtotime($this->getParam('end_time')." + 1 days"));

        $other_common = $this->otherCommon($get_start_time, $get_end_time, $searchTime);

        if (!empty($get_start_time) && !empty($get_end_time)) {
            if (strtotime($get_end_time) > strtotime($get_start_time)) {
                $where = 'bus_employee.' . $this->where[0];
                $on = " and " . "bus_employee_reward.create_time >= '$get_start_time' and bus_employee_reward.create_time <= '$get_end_time'";
            } else {
                $this->error('参数错误');
            }
        } else {
            if (!in_array($searchTime, [1, 2, 3])) {
                $this->error('信息错误');
            }

            if ($searchTime == 1) {  //  今日
                $start_time = date("Y-m-d") . ' 00:00:00';
                $end_time = date("Y-m-") . (date("d") + 1) . ' 00:00:00';
                $where = 'bus_employee.' . $this->where[0];
                $on = " and " . "bus_employee_reward.create_time >= '$start_time' and bus_employee_reward.create_time < '$end_time'";
            }
            if ($searchTime == 2) {  //  本月
                $start_time = date("Y-m-01") . ' 00:00:00';
                $end_time = date("Y-") . (date("m") + 1) . '-01' . ' 00:00:00';
                $where = 'bus_employee.' . $this->where[0];
                $on = " and " . "bus_employee_reward.create_time >= '$start_time' and bus_employee_reward.create_time < '$end_time'";
            }
            if ($searchTime == 3) {  //  本年
                $start_time = date("Y-01-01") . ' 00:00:00';
                $end_time = (date("Y") + 1) . '-01-01' . ' 00:00:00';
                $where = 'bus_employee.' . $this->where[0];
                $on = " and " . "bus_employee_reward.create_time >= '$start_time' and bus_employee_reward.create_time < '$end_time'";
            }
        }
        // 员工手工提成
        $BusEmployeeRewards = BusEmployee::find()
            ->select([
                'bus_employee.employee_name',
                'sum(bus_employee_reward.handwork_reward) as consume_reward',
            ])->leftJoin('bus_employee_reward', 'bus_employee.employee_id = bus_employee_reward.employee_id' . $on)
            ->where($where . ' and bus_employee.del_flag=1')->groupBy('bus_employee.employee_id')
            ->asArray()->all();

        return $this->render('three', ['BusEmployeeRewards' => $BusEmployeeRewards, 'other_common' => $other_common]);
    }

    // 冒泡排序
    public function maopao($arr, $type = true)
    {
        $len = count($arr);
        //从小到大
        for ($i = 1; $i < $len; $i++) {
            for ($j = $len - 1; $j >= $i; $j--) {
                if ($type == true) {
                    if ($arr[$j] < $arr[$j - 1]) { // 从小到大
                        $x = $arr[$j];
                        $arr[$j] = $arr[$j - 1];
                        $arr[$j - 1] = $x;
                    }
                } else {
                    if ($arr[$j] > $arr[$j - 1]) { // 从大到小
                        $x = $arr[$j];
                        $arr[$j] = $arr[$j - 1];
                        $arr[$j - 1] = $x;
                    }
                }
            }
        }
        return $arr;
    }

}

?>